<?php
namespace Home\Controller;

use Think\Controller;

class TopicsController extends HomeController
{

    public function index()
    {
        // 承接 导航栏 , 判定登录状态 
        if (empty(session('home'))) {
            $this->display('Login/index');
            exit;
        }

        // 判定页码以及 0级话题的id传参
        //判断分页条件
        if(!empty($_GET['p']) && intval($_GET['p']) > 0) {
            $p = intval($_GET['p']);
        } else {
            $p = 1;
        }

        // 判断查询的0级话题
        if(!empty($_GET['tid']) && intval($_GET['tid']) > 0) {
            $tid = intval($_GET['tid']);
        } else {
            $tid = 1;
        }

        // 0 级基础 话题arr表示
        $arr = array('游戏','运动','互联网','艺术','阅读','美食','动漫','汽车','生活方式','教育','摄影','历史','文化','旅行','职业发展','经济学','足球','篮球','投资','音乐','电影','法律','自然科学','设计','创业','健康','商业','体育','科技','化学','物理学','生物学','金融');
        $num = range(0,32);

        // 写查询条件 查询0级话题下的话题
        $topics = M('topics');
        $where = "(path LIKE '0,{$tid},%') OR id='{$tid}'";
        // 查询 游戏话题的子话题
        $data = $topics->field('id,name,image,attend_num,detail')->where($where)->page($p,10)->select();


        // 查询用户已经关注几个话题
        $at_num = M('user_topic')->where('user_id='.$_SESSION['home']['id'])->count('topic_id');
        if ($at_num == null) {
            $at_num = 0;
        }
        if ($data != null) {
            for ($i=0; $i < count($data); $i++) { 
                $data[$i]['detail'] = mb_substr($data[$i]['detail'], 0, 50).'...';
            }
        }

        // 页面需求数据
        $this->assign('title', '话题广场 - 知乎');
        $this->assign('at_num', $at_num);
        $this->assign('p', $p+1); // 页码
        $this->assign('tid', $tid); // 话题id
        $this->assign('arr', $arr); // 0级话题
        $this->assign('data', $data); // 下级话题显示
        $this->display('Topics/index');
    }


    public function gettopic()
    {

        if (empty($_GET['topicid'])) {
            $_GET['topicid'] = 1;
            $_GET['page'] = 1;
        }
        
        // 获取数据
        $topicid = I('get.topicid');
        $page = I('get.page');

        $topics = M('topics');
        // 写查询条件
        $where = "(path LIKE '".$topics->getFieldById($topicid,'path').$topicid.",%') OR id='{$topicid}'";
        // 查询 0级话题的子话题
        $data = $topics->field('id,name,image,attend_num')->where($where)->page($page,10)->select();

        // var_dump($data);
        // $data = [1,2];
        $this->ajaxReturn($data);
    }



    public function detail(){
        //判断分页条件
        if(!empty($_GET['p']) && intval($_GET['p']) > 0) {
            $p = intval($_GET['p']);
        } else {
            $p = 1;
        }

        // 判断话题的id
        if(!empty($_GET['tid']) && intval($_GET['tid']) > 0) {
            $tid = intval($_GET['tid']);
        } else {
            $this->error('无效的话题id...');
        }

        //查询话题的相关信息
        $topicInfo = M('topics')->field('id, name, detail, image, attend_num')->where(array('id'=>$tid))->select();

        $list = M('question')->field('id qid, name qname,user_id quid, user_name quname, answer_num qanum')->where(array('topic_id'=>$tid, 'display'=>1))->order('datetime DESC')->page($p, 10)->select(); //每页10条

        $a = M('answer');
        if (!empty($list)) {
            $i = 0;
            foreach ($list as $val) {
                // 循环内尽量不做查询
                // 但是不一定所有问题都有答案,如果关联查询,则无答案的问题会被舍去
                $arr = $a->field('id aid, part apart, zan azan, discuss_number adnum, datetime atime')->where('question_id='.$val['qid'])->limit(0,1)->order('datetime DESC')->select();
                // var_dump($arr);
                $list[$i]['aid'] = $arr[0]['aid'];
                $list[$i]['apart'] = $arr[0]['apart'];
                $list[$i]['azan'] = $arr[0]['azan'];
                $list[$i]['adnum'] = $arr[0]['adnum'];
                $list[$i]['atime'] = $arr[0]['atime'];
                $i++;
            }
        }




        // var_dump($topicInfo);
        // var_dump($list); exit;
        $this->assign('title', '话题详情 - 知乎');
        $this->assign('topicInfo',$topicInfo);
        $this->assign('list',$list);
        $this->display('Topics/detail');
    }



}